﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataManager;
using StoreClientModule;
namespace MBB_Store_Sales_Report
{
    public partial class frmChequeEntry : Form
    {
        ChequeRecords cr = new ChequeRecords();
        DataTable RecordTable = new DataTable();
        string oldCheckNumber = "";
        bool isOnAdd = false;
        
        public frmChequeEntry()
        {
            InitializeComponent();

            btnAdd.Click += new EventHandler(btnAdd_Click);
            btnCommit.Click += new EventHandler(btnCommit_Click);
            btnClose.Click += new EventHandler(btnClose_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            
            
            txtAmount.LostFocus += new EventHandler(txtAmount_LostFocus);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);
         
            cr.ProcessCompleted += new ChequeRecords.MyEventHandler(cr_ProcessCompleted);
            cr.ProcessError += new ChequeRecords.MyEventHandler(cr_ProcessError);
        }

       

        void txtAmount_LostFocus(object sender, EventArgs e)
        {
            try
            {
                double num = double.Parse(txtAmount.Text);
                txtAmount.Text = Common.FormatCurrency(num);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Invalid Input: "+ex.Message);
            }
        }
        void btnSave_Click(object sender, EventArgs e)
        {
            //check empty input
            if (txtAccntName.Text != "" && txtAmount.Text != "" && txtBankName.Text != "" && txtCheckNumber.Text != "")
            {
                
                    ChequeEntryCommit check = new ChequeEntryCommit();
                    check.oldCheckNumber = oldCheckNumber;
                    check.acctName = txtAccntName.Text;
                    check.bankName = txtBankName.Text;
                    check.dateReceived = txtDateReceived.Text;
                    check.checkNumber = txtCheckNumber.Text;
                    check.amount = txtAmount.Text;
                    check.duedate = txtDueDate.Text;
                  
                  
                    cr.CheckDuplicate(check.checkNumber);
                    cr.UpdateRecords(check);

                
            }
            else
                MessageBox.Show("Complete the Required Fields");
        }

        void btnCommit_Click(object sender, EventArgs e)
        {
            if (grd.Rows.Count > 0)
            {
                ChequeEntryCommit [] cec = new ChequeEntryCommit[grd.Rows.Count];
                for (int i = 0; i < grd.Rows.Count; i++)
                {
                    cec[i].acctName = grd.Rows[i].Cells[0].Value.ToString();
                    cec[i].bankName = grd.Rows[i].Cells[1].Value.ToString();
                    cec[i].dateReceived = grd.Rows[i].Cells[2].Value.ToString();
                    cec[i].checkNumber = grd.Rows[i].Cells[3].Value.ToString();
                    cec[i].amount = grd.Rows[i].Cells[4].Value.ToString();
                    cec[i].duedate = grd.Rows[i].Cells[5].Value.ToString();
                    cec[i].ORNumber = grd.Rows[i].Cells[6].Value.ToString();
                    cec[i].InvoiceNumber = grd.Rows[i].Cells[7].Value.ToString();

                    if (grd.Rows[i].Cells[7].Value.ToString().Equals(""))
                        cec[i].type = "Sales";
                    else
                        cec[i].type = "Charge";

                }
                pnlMsg.Visible = true;
                cr.Commit(cec);
                
            }
        }
        void btnAdd_Click(object sender, EventArgs e)
        {
                //check empty input
                if (txtAccntName.Text != "" && txtAmount.Text != "" && txtBankName.Text != "" && txtCheckNumber.Text != "" )
                {
                   
                        bool isPresent = false;
                        //check cheque number duplicate
                        for (int i = 0; i < grd.Rows.Count; i++)
                        {
                            if (txtCheckNumber.Text.Equals(grd.Rows[i].Cells[3].Value.ToString()))
                                isPresent = true;
                        }
                        if (isPresent == false)
                        {
                            isOnAdd = true;
                            cr.CheckDuplicate(txtCheckNumber.Text);
                        }
                        else
                            MessageBox.Show("Duplicate Check Number");
                   
                }
                else
                {
                    MessageBox.Show("Complete the Required Fields");
                }
            
        }
        
        void cr_ProcessError(string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                pnlMsg.Visible = false;
                MessageBox.Show(msg);
            });
        }
        void cr_ProcessCompleted(string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                //add record temporarily
                if (cr.ProcessCode == 0)
                {
                    if (cr.Duplicate == true)
                    {
                        MessageBox.Show("Duplicate Check Number ");
                    }
                    else if (cr.Duplicate == false && isOnAdd == true)
                    {
                        addCheck();
                        clearFields();
                        computeTotal();
                    }
                }
                
                if (cr.ProcessCode == 1)
                {
                    RecordTable = cr.RecordTable;
                    grd.DataSource = RecordTable;
                    computeTotal();
                }

                if (cr.ProcessCode == 2)
                {
                    RecordTable.Rows.Clear();
                    clearFields();
                    pnlMsg.Visible = false;
                    MessageBox.Show("Record/s Successfully Saved");
                }
                if (cr.ProcessCode == 3)
                {
                    pnlMsg.Visible = false;
                    if (cr.Updated == true)
                        MessageBox.Show("Updated Successfully");
                    
                }
            });
        }
        
        void computeTotal()
        {
            double tAmount = 0.0f;
            for (int i = 0; i < grd.Rows.Count; i++)
            {
                double amt = 0.0f;
                double.TryParse(grd.Rows[i].Cells[4].Value.ToString(), out amt);
                tAmount = tAmount + amt;
            }
            txtTotal.Text = Common.FormatCurrency(tAmount);
        }
        void clearFields()
        {
            txtAccntName.Text = "";
            txtAmount.Text = "";
            txtBankName.Text = "";
            txtCheckNumber.Text = "";
            txtTotal.Text = "";
            txtDueDate.Text = "";
            txtDateReceived.Text = "";
            
        }
        void addCheck()
        {
            DataRow dr = RecordTable.NewRow();
            dr["Account Name"] = txtAccntName.Text;
            dr["Name of Bank"] = txtBankName.Text;
            dr["Date Received"] = txtDateReceived.Text;
            dr["Check Number"] = txtCheckNumber.Text;
            dr["Amount"] = txtAmount.Text;
            dr["Due Date"] = txtDueDate.Text;
          
          

            RecordTable.Rows.Add(dr);
        }
        public void buttonChange02()
        {
            btnAdd.Visible= false;
            btnCommit.Visible= false;
            txtDateReceived.Enabled = true;
            btnSave.Visible = true;
        }
        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlChequeEntry.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlChequeEntry; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;

            grd.DataSource = RecordTable;
            grd.Font = new Font("Courier New", 8.5f);

            RecordTable.Columns.Add("Account Name");
            RecordTable.Columns.Add("Name of Bank");
            RecordTable.Columns.Add("Date Received");
            RecordTable.Columns.Add("Check Number");
            RecordTable.Columns.Add("Amount");
            RecordTable.Columns.Add("Due Date");
            RecordTable.Columns.Add("OR Number");
            RecordTable.Columns.Add("Invoice Number");
            
            //cr.GetRecords();
            
        }

        public void oldData(ChequeEntryCommit old)
        {
            oldCheckNumber = old.checkNumber;
            txtAccntName.Text = old.acctName;
            txtAmount.Text = old.amount;
            txtBankName.Text = old.bankName;
            txtCheckNumber.Text = old.checkNumber;
            txtDateReceived.Text = old.dateReceived;
            txtDueDate.Text = old.duedate;
          
        }
    }
}
